<template>
  <div id="app">
    <div class="container">
      <!-- 页头 -->
      <div class="header">
        <h1>公告信息列表</h1>
      </div>

      <!-- 筛选区域 -->
      <div class="filter-section">
        <div class="filter-row">
          <div class="filter-group">
            <label>所属地区</label>
            <van-dropdown-menu active-color="#1a2980">
              <van-dropdown-item v-model="filter.district" :options="districtOptions" @change="onFilterChange" />
            </van-dropdown-menu>
          </div>

          <div class="filter-group">
            <label>通告类型</label>
            <van-dropdown-menu active-color="#1a2980">
              <van-dropdown-item v-model="filter.tgType" :options="tgTypeOptions" multiple @change="onFilterChange" />
            </van-dropdown-menu>
          </div>
        </div>
      </div>

      <!-- 页脚 -->
      <div class="footer">
        <p>共 {{ total }} 条公告</p>
      </div>
      <!-- 公告列表 -->
      <div
        class="notice-list"
      >
        <van-empty
          v-if="notices.length === 0 && !loading"
          description="未找到相关公告"
          class="empty-state"
        />

        <van-list
          v-else
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          :immediate-check="false"
          @load="onLoad"
        >
          <van-cell-group>
            <van-cell v-for="(notice,index) in notices" :key="index">
              <div class="notice-card" @click="handleNoticeItem(notice)">
                <div class="notice-header">
                  <van-tag size="medium" color="#1a2980">公告</van-tag>
                  <h3>{{ notice.tgTitle }}</h3>
                </div>
                <div class="notice-meta">
                  <van-icon name="clock-o" size="14" />
                  <span>{{ notice.addTime }}</span>
                </div>
              </div>
            </van-cell>
          </van-cell-group>
        </van-list>

        <div v-if="loading" class="loading-text">加载中...</div>
      </div>
    </div>
  </div>
</template>

<script>
// import { Toast } from 'vant'
import { pushShowNoticeList } from '@/api/mobile/index'

export default {
  name: 'NoticeList',
  data() {
    return {
      // 筛选条件
      filter: {
        district: '',
        tgType: '',
        pageNo: 0,
        pageSize: 10
      },

      // 公告数据
      notices: [],

      // 分页相关
      loading: false,
      finished: false,
      total: 0,

      // 筛选选项
      districtOptions: [
        { text: '全部地区', value: '' },
        { text: '宜宾', value: '宜宾' },
        { text: '泸州', value: '泸州' },
        { text: '重庆', value: '重庆' },
        { text: '宜昌', value: '宜昌' }
      ],

      tgTypeOptions: [
        { text: '全部类型', value: '' },
        { text: '航标失常', value: '航标失常' },
        { text: '航标位置调整', value: '航标位置调整' },
        { text: '航标优化调整', value: '航标优化调整' },
        { text: '突发情况', value: '突发情况' },
        { text: '施工', value: '施工' },
        { text: '航道调整', value: '航道调整' },
        { text: '安全预警', value: '安全预警' },
        { text: '禁限航', value: '禁限航' }
      ]
    }
  },
  computed: {
    // 是否有激活的筛选条件
    hasActiveFilters() {
      return this.filter.district || this.filter.tgType
    }
  },
  activated() {
    console.log('NoticeList activated')
  },
  deactivated() {
    console.log('NoticeList deactivated')
  },
  mounted() {
    // 初始化时加载数据
    const { name } = this.$route
    if (name) {
      this.$store.dispatch('tagsView/addView', this.$route)
    }
    this.fetchNotices()
  },
  methods: {
    // 跳转到详情页
    handleNoticeItem(item) {
      this.$router.push({ path: `/notice-detial?id=${item.id}` })
    },
    // 获取公告数据
    async fetchNotices(reset = false) {
      if (reset) {
        this.filter.pageNo = 0
        this.notices = []
        this.finished = false
      }

      this.loading = true

      try {
        // 准备请求参数
        const params = {
          district: this.filter.district,
          tgType: this.filter.tgType,
          pageNo: this.filter.pageNo,
          pageSize: this.filter.pageSize
        }

        const res = await pushShowNoticeList(params)
        const data = res.content || []
        this.total = res.totalElements || 0

        if (reset) {
          this.notices = data
        } else {
          this.notices = [...this.notices, ...data]
        }

        // 检查是否加载完毕
        if (this.notices.length >= this.total) {
          this.finished = true
        } else {
          this.finished = false
        }
      } catch (error) {
        console.error('获取公告列表失败', error)
        this.$toast('获取公告列表失败')
      } finally {
        this.loading = false
      }
    },

    // 上滑加载更多
    onLoad() {
      this.filter.pageNo++
      this.fetchNotices()
    },

    // 筛选条件变化
    onFilterChange() {
      this.filter.pageNo = 0
      this.fetchNotices(true)
    },

    // 获取地区标签文本
    getDistrictLabel(value) {
      const option = this.districtOptions.find(opt => opt.value === value)
      return option ? option.text : ''
    },

    // 获取类型标签文本
    getTgTypeLabel(value) {
      const option = this.tgTypeOptions.find(opt => opt.value === value)
      return option ? option.text : ''
    }
  }
}
</script>

<style scoped>

#app {
      display: flex;
      flex-direction: column;
      height: 100%;
      max-width: 800px;
      background: white;
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
      overflow: hidden;
    }

    .container {
      display: flex;
      flex-direction: column;
      height: 100%;
    }

    /* 页头样式 */
    .header {
      background: rgb(26, 41, 128) !important;
      color: white;
      padding: 25px 20px;
      text-align: center;
      position: relative;
      overflow: hidden;
      flex-shrink: 0;
    }

    .header h1 {
      font-size: 24px;
      font-weight: 600;
      letter-spacing: 1px;
      position: relative;
      z-index: 2;
      text-shadow: 0 2px 4px rgba(0,0,0,0.2);
    }

    .header::before {
      content: "";
      position: absolute;
      top: -50%;
      left: -50%;
      width: 200%;
      height: 200%;
      background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 70%);
      z-index: 1;
    }

    /* 筛选区域样式 */
    .filter-section {
      background: white;
      padding: 16px;
      border-radius: 12px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
      margin: -20px 16px 20px;
      position: relative;
      z-index: 10;
      flex-shrink: 0;
    }

    .filter-row {
      display: flex;
      flex-wrap: wrap;
      gap: 12px;
      margin-bottom: 12px;
    }

    .filter-group {
      flex: 1;
      min-width: 150px;
    }

    .filter-group label {
      display: block;
      margin-bottom: 6px;
      font-size: 14px;
      color: #666;
      font-weight: 500;
    }

    .van-dropdown-menu {
      border-radius: 8px;
      overflow: hidden;
      box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    }

    .filter-tags {
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
      margin-top: 8px;
    }

    .filter-tag {
      background: #eef7ff;
      color: #1a7cff;
      padding: 4px 10px;
      border-radius: 16px;
      font-size: 12px;
      display: flex;
      align-items: center;
    }

    .filter-tag .van-icon {
      margin-left: 4px;
      cursor: pointer;
    }

    /* 公告列表样式 */
    .notice-list {
      flex: 1;
      overflow-y: auto;
      margin-top: 20px;
      padding-bottom: 20px;
    }

    .notice-card {
      border-radius: 12px;
      background: white;
      padding: 16px;
      box-shadow: 0 4px 12px 4px rgba(0, 0, 0, 0.04);
      transition: transform 0.3s ease, box-shadow 0.3s ease;
      border:  1px solid rgba(48, 48, 48, 0.08);
    }

    .notice-card:hover {
      transform: translateY(-3px);
      box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
    }

    .notice-header {
      display: flex;
      align-items: center;
    }

    .notice-header h3 {
      font-size: 18px;
      font-weight: 600;
      margin-left: 10px;
      color: #1a2980;
      flex: 1;
    }

    .notice-meta {
      display: flex;
      align-items: center;
      color: #666;
      font-size: 13px;
      flex-wrap: wrap;
      gap: 15px;
    }

    .notice-meta .van-icon {
      margin-right: 4px;
      vertical-align: middle;
    }

    .notice-content {
      color: #444;
      line-height: 1.6;
      font-size: 14px;
    }

    .notice-content p {
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    /* 页脚样式 */
    .footer {
      text-align: right;
      color: #888;
      padding-right: 20px;
      font-size: 14px;
      flex-shrink: 0;
      background: #f8f9fa;
      border-top: 1px solid #eee;
    }

    .empty-state {
      padding: 40px 0;
      text-align: center;
    }

    .empty-state img {
      width: 100px;
      height: auto;
      opacity: 0.7;
      margin-bottom: 15px;
    }

    /* 加载提示 */
    .loading-container {
      text-align: center;
      padding: 20px 0;
    }

    .loading-spinner {
      display: inline-block;
      width: 40px;
      height: 40px;
      border: 3px solid rgba(26, 41, 128, 0.2);
      border-radius: 50%;
      border-top-color: #1a2980;
      animation: spin 1s linear infinite;
      margin-bottom: 10px;
    }

    @keyframes spin {
      to { transform: rotate(360deg); }
    }

    .loading-text {
      color: #666;
      font-size: 14px;
    }

    .finished-text {
      text-align: center;
      padding: 16px;
      color: #999;
      font-size: 14px;
    }

    /* 响应式调整 */
    @media (max-width: 600px) {
      .filter-row {
        flex-direction: column;
      }

      .filter-group {
        width: 100%;
      }

      .notice-header {
        flex-direction: column;
        align-items: flex-start;
      }

      .notice-header h3 {
        margin-left: 0;
        margin-top: 8px;
      }

      .header {
        padding: 20px 16px;
      }

      .filter-section {
        margin: -20px 10px 15px;
      }
    }
</style>
